package com.ddyx.service.impl;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.jms.Destination;

import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.stereotype.Service;

import com.ddyx.util.EventTag;
import com.ddyx.util.MQUtil;
import com.ddyx.util.PropertiesUtil;
import com.ddyx.util.template.KeyWord;
import com.ddyx.util.template.TemplateMessageParam;
import com.ddyx.vo.TempMsgResp;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * @ClassName: TemplateMessageHandler.java
 * @Description: 此接口用于处理微信服务通知内容的处理
 * @author: lhj
 * @date: 2018年1月2日
 */
@Service
public class TemplateMessageHandler {
	// 用户的openid
	private String touser;
	// 模版信息的模版id
	private String template_id;
	// 点击模版的跳转页面
	private String page;
	// 表单提交id form_id 前端生成
	private String form_id;
	// 突出 显示 关键字
	private Object emphasis_keyword;

	public void setTouser(String touser) {
		this.touser = touser;
	}

	public void setTemplate_id(String template_id) {
		this.template_id = template_id;
	}

	public void setPage(String page) {
		this.page = page;
	}

	public void setForm_id(String form_id) {
		this.form_id = form_id;
	}

	public void setEmphasis_keyword(Object emphasis_keyword) {
		this.emphasis_keyword = emphasis_keyword;
	}

	// 用于存放模版消息keyword
	private static Map keyword = new HashMap<>();
	// 设置模版消息的参数
	private static TemplateMessageParam tmpMsgParam = new TemplateMessageParam();
	// 模版消息的消息体
	private static List list = new ArrayList<>();

	private static ObjectMapper jsonMapper = new ObjectMapper();
	@Resource
	JmsTemplate jmsTemplate;
	@Resource
	Destination tmpMsgDestination;

	/**
	 * @DESC 键值
	 * @param keyword1
	 * @param keyword2
	 * @param keyword3
	 * @param keyword4
	 * @param keyword5
	 */
	public void send(Object keyword1, Object keyword2, Object keyword3, Object keyword4, Object keyword5) {
		list.add(new KeyWord(keyword1, EventTag.BLACK));
		list.add(new KeyWord(keyword2, EventTag.BLACK));
		list.add(new KeyWord(keyword3, EventTag.BLACK));
		list.add(new KeyWord(keyword4, EventTag.BLACK));
		list.add(new KeyWord(keyword5, EventTag.BLACK));
		int i = 1;
		for (Object o : list) {
			keyword.put("keyword" + i, o);
			i++;
		}
		// 设置参数
		tmpMsgParam.setTouser(this.touser);
		tmpMsgParam.setForm_id(this.form_id);
		tmpMsgParam.setTemplate_id(this.template_id);
		tmpMsgParam.setData(keyword);
		if (this.emphasis_keyword != null) {
			tmpMsgParam.setEmphasis_keyword("keyword" + this.emphasis_keyword + ".DATA");
		}
		if (this.page != null) {
			tmpMsgParam.setPage(this.page);
		}
		try {
			String json = jsonMapper.writeValueAsString(tmpMsgParam);
			// MQ异步发送
			MQUtil.sendJsonMsg(json, jmsTemplate, tmpMsgDestination);
		} catch (JsonProcessingException e) {
			e.printStackTrace();
		}
	}
	
		public static void main(String[] args) {
			TemplateMessageHandler templateMessageHandler = new TemplateMessageHandler();
			// 模版id
			templateMessageHandler.setTemplate_id(PropertiesUtil.getProperty(EventTag.ORDERPAYTEMP));
			// formid
			templateMessageHandler.setForm_id("2c51de5a549d4bbde63d7b1a6ec15c87");
			// 微信用户 的openId
			templateMessageHandler.setTouser("openId");
			// /pages/orderDetail/orderDetail?orderId=value1&storeNo=value2    跳转页面 
			templateMessageHandler.setPage(PropertiesUtil.getProperty(EventTag.ORDERPAGE).replace("value1", "171981611783684096").replace("value2", "S00000001"));
			// 显示的内容
			templateMessageHandler.send(1, 2, 3, 4, 5);
		}
}
